package com.cl.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.cl.safe.R;
import com.cl.service.AddressService;
import com.cl.service.CallSmsSafeService;
import com.cl.service.WatchDogService;
import com.cl.ui.SettingClickView;
import com.cl.ui.SettingItemView;
import com.cl.utils.ServiceUtils;

public class SettingActivity extends Activity {
	// 设置是否开启自动更新
	private SettingItemView siv_update;
	// 设置是否开启显示归属地
	private SettingItemView siv_address;
	private SettingItemView siv_callsms_safe;
	private SettingItemView siv_lock_safe;
	private SettingClickView scv_changebg;
	private Intent showAddress;
	private SharedPreferences sp;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		showAddress = new Intent(this, AddressService.class);
		boolean isRunningserver = ServiceUtils.isRuningServer(getApplicationContext(), "com.itheima.service.AddressService");
		siv_address.setChecked(isRunningserver);
		//监听黑名单拦截的服务是开启的
		boolean isBlackNumserver = ServiceUtils.isRuningServer(getApplicationContext(), "com.itheima.service.CallSmsSafeService");
		siv_callsms_safe.setChecked(isBlackNumserver);
		//监听看门狗服务是否开启
		boolean isWatchDogserver = ServiceUtils.isRuningServer(getApplicationContext(),
				"com.itheima.service.WatchDogService");
		siv_lock_safe.setChecked(isWatchDogserver);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		siv_update = (SettingItemView) findViewById(R.id.siv_update);
		siv_address = (SettingItemView) findViewById(R.id.siv_address);
		siv_lock_safe = (SettingItemView) findViewById(R.id.siv_lock_safe);
		siv_callsms_safe = (SettingItemView) findViewById(R.id.siv_callsms_safe);
		scv_changebg = (SettingClickView) findViewById(R.id.scv_changebg);
		//是否检查更新，CheckBox的设置
		boolean isUpdate = sp.getBoolean("isUpdate", false);
		if(isUpdate) {
			siv_update.setChecked(true);
		}else {
			siv_update.setChecked(false);
		}
		siv_update.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = sp.edit();
				if(siv_update.isChecked()) {
					siv_update.setChecked(false);
					editor.putBoolean("isUpdate", false);
				}else {
					siv_update.setChecked(true);
					editor.putBoolean("isUpdate", true);
				}
				editor.commit();
			}
		});
		
		//监听来电的服务是开启的
		boolean isRunningserver = ServiceUtils.isRuningServer(getApplicationContext(), "com.itheima.service.AddressService");
		if(isRunningserver) {
			siv_address.setChecked(true);
		}else {
			siv_address.setChecked(false);
		}
		showAddress = new Intent(this,AddressService.class);
		siv_address.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(siv_address.isChecked()) {
					siv_address.setChecked(false);
					stopService(showAddress);
				}else {
					siv_address.setChecked(true);
					startService(showAddress);
				}
			}
		});
		
		//监听黑名单拦截的服务是开启的
		boolean isBlackNumserver = ServiceUtils.isRuningServer(getApplicationContext(), "com.itheima.service.CallSmsSafeService");
		siv_callsms_safe.setChecked(isBlackNumserver);
		siv_callsms_safe.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent callSmsSafeService = new Intent(SettingActivity.this,CallSmsSafeService.class);
				if(siv_callsms_safe.isChecked()) {
					siv_callsms_safe.setChecked(false);
					stopService(callSmsSafeService);
				}else {
					siv_callsms_safe.setChecked(true);
					startService(callSmsSafeService);
				}
			}
		});
		
		//监听程序锁的服务是开启的
		boolean isWatchDogserver = ServiceUtils.isRuningServer(getApplicationContext(),
				"com.itheima.service.WatchDogService");
		siv_lock_safe.setChecked(isWatchDogserver);
		siv_lock_safe.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent watchDogService = new Intent(SettingActivity.this,WatchDogService.class);
				if(siv_lock_safe.isChecked()) {
					siv_lock_safe.setChecked(false);
					stopService(watchDogService);
				}else {
					siv_lock_safe.setChecked(true);
					startService(watchDogService);
				}
			}
		});
		
		scv_changebg.setTitle("归属地提示框风格");
		final String [] items = {"半透明","活力橙","卫士蓝","金属灰","苹果绿"};
		int which = sp.getInt("which", 0);
		scv_changebg.setDesc(items[which]);
		scv_changebg.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int dd = sp.getInt("which", 0);
				// 弹出一个对话框
				AlertDialog.Builder builder = new Builder(SettingActivity.this);
				builder.setTitle("归属地提示框风格");
				builder.setSingleChoiceItems(items,dd, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						//保存选择参数
						Editor editor = sp.edit();
						editor.putInt("which", which);
						editor.commit();
						scv_changebg.setDesc(items[which]);
						
						//取消对话框
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("cancel", null);
				builder.show();
				
			}
		});
	}
	
	
}
